package com.mtconnect.util;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
import static java.time.temporal.ChronoField.*;

/**
 * Created by Administrator on 2017/8/1.
 * 日期工具类
 */
public class DateUtil {


    /**
     * 取得UTC时间的字符串格式 yyyy-MM-ddTHH:mm:ssZ
     *
     * 例如：
     *      2017-09-06T07:30:11Z
     *
     * @return UTC时间的字符串格式
     */
    public static String getUTCTime() {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .append(ISO_LOCAL_DATE)
                .appendLiteral('T')
                .appendValue(HOUR_OF_DAY, 2)
                .appendLiteral(':')
                .appendValue(MINUTE_OF_HOUR, 2)
                .optionalStart()
                .appendLiteral(':')
                .appendValue(SECOND_OF_MINUTE, 2)
                .optionalStart()
                .appendLiteral('Z').toFormatter();
        ZoneId utc = ZoneId.of("UTC");
        return LocalDateTime.now(utc).format(formatter);
    }

    public static String getUTCFormat(LocalDateTime localDateTime){
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .append(ISO_LOCAL_DATE)
                .appendLiteral('T')
                .appendValue(HOUR_OF_DAY, 2)
                .appendLiteral(':')
                .appendValue(MINUTE_OF_HOUR, 2)
                .optionalStart()
                .appendLiteral(':')
                .appendValue(SECOND_OF_MINUTE, 2)
                .optionalStart()
                .appendLiteral('Z').toFormatter();
        return localDateTime.format(formatter);
    }

    /**
     * 取得UTC时间的毫秒数
     *
     * @return UTC时间的毫秒数
     */
    public static long getUTCTimeInMillis() {
        Calendar cal = Calendar.getInstance();
        int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
        int dstOffset = cal.get(Calendar.DST_OFFSET);
        cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        return cal.getTimeInMillis();
    }

    public static String getISO8601Format(Date timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS Z");
        format.setTimeZone(TimeZone.getTimeZone("UTC"));
        return format.format(timestamp);
    }

    /**
     * 取得当前utc时间
     *
     * @return 返回当前的UTC时间，按照ISO8601格式
     */
    public static String getNowTime2ISO8601Format() {
        Calendar utc = Calendar.getInstance();
        utc.setTimeInMillis(getUTCTimeInMillis());
        String iso8601Format = getISO8601Format(utc.getTime());
        String[] split = iso8601Format.split(" ");
        return split[0] + " +0000";
    }

    public static void main(String[] args) {

    }

}
